<template>
  <view class="goods-detail" v-if="detail.pics">
    <swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item,index) in detail.pics" :key="index">
        <view class="swiper-item" @click="previewImg(index)">
          <image class="pics" :src="item.pics_big" mode="widthFix"></image>
        </view>
      </swiper-item>
    </swiper>
    <!-- 商品信息 -->
    <view class="message">
      <view class="price">
        ￥{{detail.goods_price}}
      </view>
      <view class="info">
        <view class="left">
          {{detail.goods_name}}
        </view>
        <view class="right">
          <uni-icons type="star" size="22"></uni-icons>
          收藏
        </view>
      </view>
      <view class="pass">
        快递：免运费
      </view>
    </view>
    <!-- 详情富文本
     富文本样式处理：通过字符替换解决
     
     -->
    <rich-text :nodes="detail.goods_introduce"></rich-text>
    <view class="bottom">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapGetters
  } from 'vuex'

  export default {
    data() {
      return {
        detail: '',
        options: [{
            icon: 'shop',
            text: '店铺'
          },
          {
            icon: 'cart',
            text: '购物车',
            info: 4,
            infoBackgroundColor: '#ff0000',
            infoColor: "#fff"
          }
        ],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    computed: {
      ...mapGetters('cart', ['total'])
    },
    // watch:某个值的change事件
    // deep,immediate
    // watch的vue3:可以侦听多个值的变化，返回了一个stop:可以停止侦听，flush:pre:默认是渲染前执行回调,post:渲染后执行回调,sync:修改一次执行一次回调（渲染前）
    watch: {
      total: {
        handler(newVal) {
          this.options[1].info = newVal
        },
        immediate: true
      }
    },
    onLoad(options) {
      this.getData(options.goods_id)
    },
    methods: {
      async getData(id) {
        const {
          message
        } = await uni.$http.get('/goods/detail', {
          params: {
            goods_id: id
          }
        })
        // 富文本在小程序无法通过css穿透解决，解决方式：文本替换
        message.goods_introduce = message.goods_introduce.replace(/<img /g, '<img style="display:block" ')
        message.goods_introduce = message.goods_introduce.replace(/.webp/g, '.jpg')
        this.detail = message
        console.log('详情数据', message);
      },
      // 预览图片
      previewImg(index) {
        // [{pics_big:'xxx'}]===['xxx']
        const urls = this.detail.pics.map(i => i.pics_big)
        uni.previewImage({
          current: index,
          urls: urls
        })
      },
      // 底部左边导航点击事件
      onClick(e) {
        if (e.index === 1) {
          uni.switchTab({
            url: "/pages/cart/cart"
          })
        }
        console.log(e);
      },
      // 底部右边导航点击事件
      buttonClick(e) {
        console.log(e);
        if (e.index === 0) {
          // this.detail.goods_count=1
          this.$store.commit('cart/addToCart', {
            ...this.detail,
            goods_count: 1, // 购物数量
            goods_status: true // 在购物车中是否选中状态
          })
        }
      }
    }
  }
</script>

<style lang="scss">
  .goods-detail {
    .swiper {
      height: 750rpx;

      .pics {
        width: 100%;
        height: 750rpx;
      }
    }
  }

  .message {
    padding: 0 20rpx;

    .price {
      font-size: 20px;
      color: #c00000;
      padding: 40rpx 0;
    }

    .info {
      font-size: 14px;
      display: flex;

      .left {
        flex: 1;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box; //将对象作为弹性伸缩盒子模型显示。
        -webkit-box-orient: vertical; //从上到下垂直排列子元素（设置伸缩盒子的子元素排列方式）
        -webkit-line-clamp: 2; //这个属性不是css的规范属性，需要组合上面两个属性，表示显示的行数。此处为2行
        word-wrap: break-word; //允许单词内断句，首先会尝试挪到下一行，看看下一行的宽度够不够，不够的话就进行单词内的断句
      }

      .right {
        width: 160rpx;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border-left: 1px solid #eeece9;
      }
    }

    .pass {
      font-size: 12px;
      color: #878787;
      padding: 40rpx 0;
    }
  }

  .goods-detail {
    padding-bottom: 90rpx;
  }

  .bottom {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    z-index: 999;
  }
</style>